package com.mall507.fivepeaks.activity.main;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.AbsRecyclerViewUI;
import com.hzh.frame.widget.rxbus.RxBus;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.hzh.frame.widget.xrefresh.XSwipeRefreshLayout;
import com.mall507.comn.model.Specialty;
import com.mall507.comn.model.Tour;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.activity.community.CommunityAssociationInfoActivityRUI;
import com.mall507.fivepeaks.widget.imageview.ExpandImageView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页旅游列表
 */
@Route(path = "/main/MainTourRUI")
public class MainTourRUI extends AbsRecyclerViewUI<Tour> {
    public final static int REQUEST_OK = 0xff01;
    public final static int COLLECTION_RESULT = 0xff02;//收藏回调
    public final static int COMMENT_RESULT = 0xff03;//评论回调
    public final static int VIEWS_RESULT = 0xff04;//浏览回调
    private int pos = 0;

    @Override
    protected int setLayoutId() {
        return R.layout.ui_rv_main_tour;
    }

    @Override
    protected void bindView() {
        getTitleView().setContent("五峰旅游");
        showLoding();
    }

    @Override
    protected String setHttpPath() {
        return "Tourism/tourismList";
    }

    @Override
    protected List<Tour> handleHttpData(JSONObject response) {
        List<Tour> list = new ArrayList<>();
        if (200 == response.optInt("code")) {
            JSONArray tours = response.optJSONArray("data");
            if (tours != null && tours.length() > 0) {
                for (int i = 0; i < tours.length(); i++) {
                    JSONObject object = tours.optJSONObject(i);
                    JSONArray imgs=object.optJSONArray("imgs");
                    Tour model = new Tour();
                    model.setNid(object.optString("id"));
                    if(imgs!=null && imgs.length()>0){
                        model.setImage(imgs.optString(0));
                    }else{
                        model.setImage("");
                    }
                    model.setTitle(object.optString("title"));
                    model.setAddress(object.optString("address"));
                    model.setBrowseNumber(object.optString("views"));
                    model.setCollectionNumber(object.optString("collection_count"));
                    model.setCommentNumber(object.optString("comment_count"));
                    list.add(model);
                }
            }
        }
        dismissLoding();
        return list;
    }
    
    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_main_tour;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, final int position, final Tour model) {
        ((ExpandImageView) holder.getView(R.id.image)).setImageURI(model.getImage());
        holder.setText(R.id.title, model.getTitle());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            holder.setText(R.id.address, String.valueOf(Html.fromHtml(model.getAddress(),Html.FROM_HTML_MODE_COMPACT)));
        }else {
            holder.setText(R.id.address, String.valueOf(Html.fromHtml(model.getAddress())));
        }
        holder.setText(R.id.browseNumber, model.getBrowseNumber() + "浏览");
        holder.setText(R.id.collectionNumber, model.getCollectionNumber() + "收藏");
        holder.setText(R.id.commentNumber, model.getCommentNumber() + "评论");
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pos = position;
                ARouter.getInstance().build("/main/MainTourInfoRUI")
                        .withString("type","1")
                        .withString("id", model.getNid())
                        .withString("collection_count",model.getCollectionNumber())
                        .navigation();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_OK && (resultCode == COLLECTION_RESULT || resultCode == COMMENT_RESULT)) {
            int position=data.getIntExtra("position",0);
            Tour model=getAdapter().getDatalist().get(position);
            switch (resultCode) {
                case COLLECTION_RESULT://收藏
                    String collectionNumber=data.getStringExtra("collection_count");
                    model.setCollectionNumber(collectionNumber);
                    break;
                case COMMENT_RESULT://评论
                    String commentNumber=data.getStringExtra("commentNumber");
                    model.setCommentNumber(commentNumber);
                    break;
                case VIEWS_RESULT://浏览
                    String viewsNumber=data.getStringExtra("viewsNumber");
                    model.setBrowseNumber(viewsNumber);
                    break;
            }
            getAdapter().notifyItemChanged(position);
        }
    }

    @Override
    protected void onRestart() {
        onRefresh();
        super.onRestart();
    }
}
